<?

/**
 * $Id: SharedController.php 1254 2008-09-09 08:39:51Z lule $
 *
 * Kontroler elementow wspolnych
 *
 */
class SharedController extends Controller {
	
	
	/**
	 * Glowna metoda
	 *
	 */
	public function Index() {
	}

	/*
	 * nagówek strony
	 */
	public function Header($param)
	{
		
		
	}

	public function LatestGaleries($param) {
		
	}
	
	public function MessagesCount($param) {
		
	}


	public function MyList($param) {
		$productCompareArray = SessionProxy::GetValue('productCompareArray');
		if(!is_array($productCompareArray)) {
			$productCompareArray = array();
		}
		$values = Utils::GetArrayValuesRecursive($productCompareArray);
		$products0 = array();
		$alreadyInCompare = array();
		foreach($values as $v) {
			$alreadyInCompare[$v] = $v;
		}
		$this->smarty->assign('alreadyInCompare', $alreadyInCompare);
		Registry::Set('alreadyInCompare', $alreadyInCompare);
		if(count($values)>0) {
		 $dalObj = ProductDAL::GetDalDataObj();
		 $dalObj->addCondition('product.slug', '("'.implode('","', $values).'")', ' IN ');
		 $dalObj->addJoin('ProductDescription', ' LEFT JOIN product_description ON(product.id_product = product_description.id_product AND product_description.id_language = '.Registry::Get('default_language_id').')');
		 $dalObj->addJoin('Manufacturer' , ' LEFT JOIN manufacturer ON (product.id_manufacturer = manufacturer.id_manufacturer AND manufacturer.active = 1)');
		 $products0 = ProductDAL::GetResult($dalObj);

		 $products = array();
		 $p = 0;
		 foreach($products0 as $prod) {
			 $products[$prod->getSlug()] = $prod;
			 $p++;
		 }

		 foreach($productCompareArray as $category=>$cv) {
			 $dalObj = CategoryDAL::GetDalDataObj();
		 $dalObj->addJoin('CategoryDescription', ' LEFT JOIN category_description ON (category.id_category = category_description.id_category AND category_description.id_language = 1)');
			 $dalObj->addCondition('slug', $category);
			 $result = CategoryDAL::GetResult($dalObj);
			 if(isset($result[0]))
			 $productCompareArray[$category]['name'] = $result[0];

			 foreach($cv as $subCategory=>$product) {
				 $dalObj = CategoryDAL::GetDalDataObj();
				$dalObj->addJoin('CategoryDescription', ' LEFT JOIN category_description ON (category.id_category = category_description.id_category AND category_description.id_language = 1)');
				 $dalObj->addCondition('slug', $subCategory);
			 $result = CategoryDAL::GetResult($dalObj);
			 if(isset($result[0]))
				 $productCompareArray[$category]['values'][$subCategory]['name'] = $result[0];
				 $compareLink = '';
				 foreach($product as $p=>$v) {
					 if(isset($products[$v])) {
						$productCompareArray[$category]['values'][$subCategory]['values'][$v] = $products[$v];
						$compareLink .='_'.$products[$v]->getId();
					 }
				 }
				 $productCompareArray[$category]['values'][$subCategory]['compareLink'] = $compareLink;

				
			 }
		 }

		 //var_dump($productCompareArray);
		 //echo count($products);
//var_dump(count($products));
}
			 $count = count($products0);

			 //if($count == ''){
			//	 $count = '0';
			// }


		 $this->smarty->assign('myListNumProducts', $count);
		 $this->smarty->assign('products', $productCompareArray);
		 $this->smarty->assign('lang_id', Registry::Get('default_language_id'));
	}
	
	public function InfoFrame($param)
	{
		if(is_array($param))
		{
			$this->smarty->assign('error',$param["type"]);
			$this->smarty->assign('info',$param["info"]);
		}
		else
		{
			$this->smarty->assign('error',false);
			$this->smarty->assign('info',$param);
		}
	}
	
	/*
	 * stopka strony
	 */
	public function Footer($param)
	{
		
	}

	public function CategoriesAndBrands($param)
	{
		$def_lang_id = 1;

		$this->smarty->assign('categoryKey', isset($param['categoryKey'])?$param['categoryKey']:null);
		$this->smarty->assign('subCategoryKey', isset($param['subCategoryKey'])?$param['subCategoryKey']:null);
		$this->smarty->assign('categoryTree', CategoryDAL::getCategoryTree($def_lang_id));
		
		$this->smarty->assign('brandsTree', ManufacturerDAL::GetManufacturerTree($def_lang_id));
		
		
	}
    
	/**
	 * 
	 * @param type $performedAction name z dict_actions (db)
	 * @param type $idUser 
	 */
	public function addActivityPoints($param) {
		try {
			$performedAction = $param['action'];
			$idUser = $param['idUser'];
			
			if ($idUser < 1) {
				throw new UserException('No User');
			}

			$userActivityPoints = new UserActivity();
			$activityActions = $userActivityPoints->setActivityPointsToAction();
			$activityActions = $activityActions[$performedAction];

			$userActivityPoints->setIdUser($idUser);
			$userActivityPoints->setIdDictActions($activityActions['id']);
			$userActivityPoints->setPoints($activityActions['points']);
			$userActivityPoints->setDate(date('Y-m-d H:i:s'));

			UserActivityDAL::Save($userActivityPoints->GetDalDataObj());

			if(isset($activityActions['wall']) && $activityActions['wall'] == 1) {
				$usersWall = new UsersWall();
				$keys = isset($param['tpl_vars'])?array_keys($param['tpl_vars']):array();
				if(empty($keys))return;
				sort($keys);
				$tpl_vars = isset($param['tpl_vars'])?serialize($param['tpl_vars']):serialize(array());
				$usersWall->setIdParent(0);
				$usersWall->setIdUser($idUser);
				$usersWall->setGlobalUsers(0);
				$usersWall->setDate(date('Y-m-d G:i:s',time()));
				$usersWall->setParam($tpl_vars);
				$usersWall->setDescription('action_tpl_'.$performedAction.'_'.implode(';',$keys));
				$usersWall->setIsActivity(1);

				UsersWallDAL::Save($usersWall->getDalDataObj());
			}
		} catch (Exception $e) {
			//var_dump($e->getMessage());die();
		}
	}
	
	
	public function postDispatch($param) {

	}

	public function preDispatch()
	{
		

	}

	public function Wall($param) {
		if($param['userLoggedIn']) {
			$this->addScript('wall.js','file','top',5);
		}
	}
	
	public function UserInfo($param)
	{
		if(isset($param['userId']) && $param['userId']) 
		{
			$user = UserDAL::GetResult(
					UserDAL::GetDalDataObj(
						array(
						'condition' => array(
							'id' => array($param['userId'])							
							)
						)
					)
				);
			
			$user = $user[0];
			
			$obj = UserExtendedProfileDAL::GetResult(
				UserExtendedProfileDAL::GetDalDataObj(
					array(
						'condition' => array(
							'id_user' => array($param['userId'])
						)
					)
				)
			);			
			
			$dalObj = CommentsDAL::GetDalDataObj();
			$dalObj->addCondition('id_user', $user->getId());
			$dalObj->addCondition('type', Comments::PRODUCT, '!=');
			$dalObj->setCount('id_comments');

			$user->setCommentsCount(CommentsDAL::GetResult($dalObj));

			$dalObj = CommentsDAL::GetDalDataObj();
			$dalObj->addCondition('id_user', $user->getId());
			$dalObj->addCondition('type', Comments::PRODUCT);
			$dalObj->setCount('id_comments');

			$user->setProductCommentsCount(CommentsDAL::GetResult($dalObj));

			$dalObj = UserConnectionsDAL::GetDalDataObj();
			$dalObj->addCondition('id_user', $user->getId());
			$dalObj->addCondition('active', '1');
			$dalObj->setCount('id_user_connections');

			$user->setConnectionsCount(UserConnectionsDAL::GetResult($dalObj));			
			
			$this->smarty->assign('user', $user);
			$this->smarty->assign('userExtended', $obj[0]);
		} else {
			$this->smarty->assign('user', false);
		}
	}
	
	public function BadWords($string) {
		try {
			$dal = BadWordsDAL::GetDalDataObj(array(
						'tableData' => $this->tableParams['bad_words'],
						'condition' => array('active' => 1)
					));

			$list = BadWordsDAL::GetResult($dal);
			var_dump($list);
		} catch (Exception $e) {
			
		}
	}
	
}

